from django.shortcuts import render,HttpResponse
from user.models import Img
import os,random
import cv2
import sys
import dlib
import cv2
import os
import glob

# Create your views here.

def index(request):
    return render(request,'index.html')

def upimg(request):
    if request.method == 'POST':
        img = Img(img_url=request.FILES.get('img'))
        img.save()
    return render(request,'index.html')

def upimg2(request):
    if request.method == "POST":  # 请求方法为POST时，进行处理
        rd = random.randint(10000000, 99999999)
        filelist = []
        for i in range(2):
            myfile = request.FILES.get("myfile%d" % (i), None)  # 获取上传的文件，如果没有文件，则默认为None
            if not myfile:
                continue
            tmpfile = "tmp%d_%s" % (rd, myfile.name)
            destination = open(tmpfile, 'wb+')  # 打开特定的文件进行二进制的写操作
            for chunk in myfile.chunks():  # 分块写入文件
                destination.write(chunk)
            destination.close()
            filelist += [tmpfile]
            print(tmpfile)
        print("file num = %d" % (len(filelist)))
        if len(filelist) <= 0:
            return HttpResponse("no files for upload!")
        resfile = "test%d.txt" % (rd)
        response = HttpResponse(resfile)
        return response

def bidui():
    current_path = os.getcwd()
    predictor_path ="shape_predictor_68_face_landmarks.dat"
    face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"
    # 测试图片路径
    faces_folder_path = current_path + "\\faces\\"

    # 读入模型
    detector = dlib.get_frontal_face_detector()
    shape_predictor = dlib.shape_predictor(predictor_path)
    face_rec_model = dlib.face_recognition_model_v1(face_rec_model_path)

    for img_path in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
        print("Processing file: {}".format(img_path))
        # opencv 读取图片，并显示
        img = cv2.imread(img_path, cv2.IMREAD_COLOR)
        # opencv的bgr格式图片转换成rgb格式
        b, g, r = cv2.split(img)
        img2 = cv2.merge([r, g, b])

        dets = detector(img, 1)  # 人脸标定
        print("Number of faces detected: {}".format(len(dets)))

        for index, face in enumerate(dets):
            print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(),
                                                                         face.bottom()))

            shape = shape_predictor(img2, face)  # 提取68个特征点
            for i, pt in enumerate(shape.parts()):
                # print('Part {}: {}'.format(i, pt))
                pt_pos = (pt.x, pt.y)
                cv2.circle(img, pt_pos, 2, (255, 0, 0), 1)
                # print(type(pt))
            # print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
            cv2.namedWindow(img_path + str(index), cv2.WINDOW_AUTOSIZE)
            cv2.imshow(img_path + str(index), img)

            face_descriptor = face_rec_model.compute_face_descriptor(img2, shape)  # 计算人脸的128维的向量
            print(face_descriptor)
            return face_descriptor

def duibi():
    current_path = os.getcwd()  # 获取当前路径
    # 模型路径
    predictor_path = "shape_predictor_68_face_landmarks.dat"
    face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"
    #测试图片路径
    faces_folder_path = current_path + "\\faces\\"

    # 读入模型
    detector = dlib.get_frontal_face_detector()
    shape_predictor = dlib.shape_predictor(predictor_path)
    face_rec_model = dlib.face_recognition_model_v1(face_rec_model_path)

    for img_path in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
    print("Processing file: {}".format(img_path))
    # opencv 读取图片，并显示
    img = cv2.imread(img_path, cv2.IMREAD_COLOR)
    # opencv的bgr格式图片转换成rgb格式
    b, g, r = cv2.split(img)
    img2 = cv2.merge([r, g, b])

    dets = detector(img, 1)   # 人脸标定
    print("Number of faces detected: {}".format(len(dets)))

    for index, face in enumerate(dets):
        print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom()))

        shape = shape_predictor(img2, face)   # 提取68个特征点
        for i, pt in enumerate(shape.parts()):
            #print('Part {}: {}'.format(i, pt))
            pt_pos = (pt.x, pt.y)
            cv2.circle(img, pt_pos, 2, (255, 0, 0), 1)
            #print(type(pt))
        #print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
        cv2.namedWindow(img_path+str(index), cv2.WINDOW_AUTOSIZE)
        cv2.imshow(img_path+str(index), img)

        face_descriptor = face_rec_model.compute_face_descriptor(img2, shape)   # 计算人脸的128维的向量
        print(face_descriptor)
    return face_descriptor


